package backpage.controller;

import backpage.common.Result;
import backpage.entity.Department;
import backpage.entity.Employee;
import backpage.service.DepartmentService;
import backpage.common.Result; // 确保引入正确的 Result 类
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 查询全部员工信息
     * author 吴文钦
     * @return
     */
    @GetMapping("/queryAllDeptInfo")
    public Result<List<Department>> queryAllDeptInfo() {
        List<Department> departments = departmentService.queryAllDeptInfo();
        if (!departments.isEmpty()) {
            return Result.success(departments);
        } else {
            return Result.failure(404, "未找到员工", null);
        }
    }
    /**
     * 根据ID获取部门信息
     * @param departmentId 部门ID
     * @return 部门信息
     */
    @GetMapping("/queryDeptInfoById/{departmentId}")
    public Result<Department> queryDeptInfoById(@PathVariable("departmentId") Integer departmentId) {
        Department department = departmentService.queryDeptInfoById(departmentId);
        return department != null
                ? Result.success(department)
                : Result.failure(404, "部门不存在", null);
    }

    /**
     * 检查部门编号是否存在
     * @param departmentCode 部门编号
     * @return 是否唯一
     */
    @GetMapping("/checkDeptCodeUnique/{departmentCode}")
    public Result<Boolean> checkDeptCodeUnique(@PathVariable("departmentCode") String departmentCode) {
        boolean isUnique = departmentService.checkDeptCodeUnique(departmentCode);
        return isUnique
                ? Result.success(true)
                : Result.success( false);
    }

    /**
     * 检查部门名称是否存在
     * @param departmentName 部门名称
     * @return 是否唯一
     */
    @GetMapping("/checkDeptNameUnique/{departmentName}")
    public Result<Boolean> checkDeptNameUnique(@PathVariable("departmentName") String departmentName) {
        boolean isUnique = departmentService.checkDeptNameUnique(departmentName);
        if(isUnique)
            return Result.success(true);
        else
            return Result.success(false);
    }

    /**
     * 新增部门信息
     * @author 吴文钦
     * @param departmentCode 部门编号
     * @param departmentName 部门名称
     * @param remarks 备注信息
     * @return 操作结果
     */
    @PostMapping("/insertDeptInfo/{departmentCode}/{departmentName}/{remarks}")
    public Result<?> insertDeptInfo(@PathVariable("departmentCode") String departmentCode,
                                    @PathVariable("departmentName") String departmentName,
                                    @PathVariable("remarks") String remarks) {
        try {
            Department department = new Department();
            department.setDepartmentCode(departmentCode); // 设置部门编号
            department.setDepartmentName(departmentName); // 设置部门名称
            department.setRemarks(remarks); // 设置备注信息
            department.setDepartmentStaffNumber(0); // 设置部门现有人数
            department.setDepartmentHistoryStaffNumber(0); // 设置部门创建初至今加入的人数
            boolean result = departmentService.insertDeptInfo(department);
            return result
                    ? Result.success("部门添加成功")
                    : Result.failure(500, "部门添加失败", null);
        } catch (Exception e) {
            return Result.failure(500, "服务器内部错误: " + e.getMessage(), null);
        }
    }

    /**
     * 更新部门信息
     * @param department 部门对象
     * @return 操作结果
     */
    @PutMapping("/updateDeptInfo")
    public Result<?> updateDeptInfo(@RequestBody Department department) {
        try {
            boolean result = departmentService.updateDeptInfo(department);
            return result
                    ? Result.success("部门更新成功")
                    : Result.failure(500, "部门更新失败", null);
        } catch (Exception e) {
            return Result.failure(500, "服务器内部错误: " + e.getMessage(), null);
        }
    }
    @GetMapping("/queryDeptByPage")
    public Result<IPage<Department>> queryDeptByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            IPage<Department> page = departmentService.queryDeptByPage(pageNum, pageSize);
            return Result.success(page);
        } catch (Exception e) {
            return Result.failure(500, "分页查询部门信息失败: " + e.getMessage(), null);
        }
    }

}